﻿using System;
using System.Configuration;

namespace Ue.Core.Configuration
{
    /// <summary>
    /// ElementCollection的一些基础操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ConfigurationElementCollection<T> : ConfigurationElementCollection
        where T : ConfigurationElement
    {
        public object GetElementValue(string name)
        {
            return base[name];
        }
        public void SetElementValue(string name,object value)
        {
            base[name] = value;
        }

        public new T this[string name]
        {
            get { return this.BaseGet(name) as T; }
        }

        public T this[int index]
        {
            get { return (T)this.BaseGet(index); }
            set
            {
                if (this.BaseGet(index) != null)
                {
                    this.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

        public bool ContainsKey(string name)
        {
            bool result = false;
            object[] keys = this.BaseGetAllKeys();
            foreach (object key in keys)
            {
                if ((string)key == name)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }
}
